package com.sluggard.security.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sluggard.common.model.res.Result;
import com.sluggard.mp.model.req.IdsReq;
import com.sluggard.mp.mvc.BaseController;
import com.sluggard.security.manage.model.entity.MenuEntity;
import com.sluggard.security.manage.model.req.MenuReq;
import com.sluggard.security.manage.model.res.MenuRes;
import com.sluggard.security.manage.service.IMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author lizheng
 * @since 2024-04-28
 */
@Tag(name = "菜单管理")
@RestController
@RequestMapping("/manage/system/menu")
public class MenuController extends BaseController<MenuReq, MenuEntity, IMenuService> {

    @Override
    protected List<MenuEntity> onList(QueryWrapper<MenuEntity> queryWrapper) {
        return service.tree(queryWrapper);
    }

    @Operation(summary = "树形列表查询")
    @PostMapping("tree")
    public Result<List<MenuRes>> tree(){
        return Result.ok(service.tree());
    }

    @Override
    protected void beforeSave(MenuReq req, MenuEntity instance) {
        super.beforeSave(req, instance);
        if (req.getParentId() != null) {
            MenuEntity menuEntity = service.getById(req.getParentId());
            if (Objects.isNull(menuEntity)) {
                instance.setParentId(null);
            }
        }else{
            instance.setParentId(null);
        }
    }

    @Override
    protected void onDelete(IdsReq idsReq) {
        service.onDelete(idsReq.getIds());
    }
}
